<?php

    /**
     * User: Viet Anh
     * Date: 30/05/2014
     * Time: 16:46
     */

    require_once dirname(dirname(__FILE__)) . '/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';

    class dao_document_manage
    {

        public function __construct()
        {

        }

        /**
         * @param $user_id
         *
         * @return array invoices of user
         */
        public function get_invoice_by_user_id($user_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * FROM tbl_invoice WHERE user_id = " . $user_id;
            $result = mysqli_query($con, $query) or die('Failed to get invoices by user id!' . mysqli_error());
            $list = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }

            $db->close_connect();

            return $list;
        }

        /**
         * @param $user_id
         *
         * @return array of paid invoices by user
         */
        public function get_paid_invoice_by_user_id($user_id)
        {
            $all_invoice   = $this->get_invoice_by_user_id($user_id);
            $paid_invoices = array();
            foreach ($all_invoice as $invoice) {
                if ($invoice['inv_status_id'] == 2) {
                    array_push($paid_invoices, $invoice);
                }
            }

            return $paid_invoices;
        }

        /**
         * @param $user_id
         *
         * @return array of unpaid invoices by user
         */
        public function get_unpaid_invoice_by_user_id($user_id)
        {
            $all_invoice   = $this->get_invoice_by_user_id($user_id);
            $paid_invoices = array();
            foreach ($all_invoice as $invoice) {
                if ($invoice['inv_status_id'] == 1) {
                    array_push($paid_invoices, $invoice);
                }
            }

            return $paid_invoices;
        }

        /**
         * @param $invoice_id
         *
         * @return array of invoice lines by invoice
         */
        public function get_invoice_lines_by_inv_id($invoice_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * FROM tbl_invoice_line WHERE invoice_id = " . $invoice_id;
            $result = mysqli_query($con, $query) or die('Failed to get invoice lines by invoice id!' . mysqli_error());
            $list = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }

            $db->close_connect();

            return $list;
        }

        public function get_list_all_docs_by_user($user_id)
        {
            $all_invoices = $this->get_invoice_by_user_id($user_id);
            $all_docs     = array();
            foreach ($all_invoices as $invoice) {
                $invoice_lines = $this->get_invoice_lines_by_inv_id($invoice['invoice_id']);
                foreach($invoice_lines as $line){
                    array_push($all_docs, $line['doc_id']);
                }
            }

            return $all_docs;
        }

        /**
         * @param $user_id
         *
         * @return array of documents id user bought
         */
        public function get_list_processed($user_id)
        {
            $paid_invoices = $this->get_paid_invoice_by_user_id($user_id);
            $bought_docs   = array();
            foreach ($paid_invoices as $invoice) {
                $invoice_lines = $this->get_invoice_lines_by_inv_id($invoice['invoice_id']);
                foreach ($invoice_lines as $line) {
                    array_push($bought_docs, $line['doc_id']);
                }
            }

            return $bought_docs;
        }

        /**
         * @param $user_id
         *
         * @return array of documents user unpaid
         */
        public function get_list_processing($user_id)
        {
            $unpaid_invoices    = $this->get_unpaid_invoice_by_user_id($user_id);
            $not_available_docs = array();
            foreach ($unpaid_invoices as $invoice) {
                $invoice_lines = $this->get_invoice_lines_by_inv_id($invoice['invoice_id']);
                foreach ($invoice_lines as $line) {
                    array_push($not_available_docs, $line['doc_id']);
                }
            }

            return $not_available_docs;
        }
    }